package com.aries.remoteplusserver.core;

import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

import com.illposed.osc.OSCListener;
import com.illposed.osc.OSCMessage;
import com.illposed.osc.OSCPortIn;

public class OSCHandler {

	private OSCPortIn receiver;
	
	private BasicInputHandler basicInputHandler;
	private KeyboardHandler keyboardHandler;
	private NumPadHandler numPadHandler;
	private NavigationHandler navigationHandler;
	private MediaHandler mediaHandler;
	private PowerHandler powerHandler;
	
	public OSCHandler()
	{
		try
		{
			this.basicInputHandler 	= new BasicInputHandler();
			this.keyboardHandler 	= new KeyboardHandler();
			this.numPadHandler 		= new NumPadHandler();
			this.navigationHandler	= new NavigationHandler();
			this.mediaHandler		= new MediaHandler();
			this.powerHandler		= new PowerHandler();
		} catch (AWTException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void onMessageIn()
	{
		try {
			InetAddress localAddress = InetAddress.getLocalHost();
			
			if ( localAddress.isLoopbackAddress() ) 
			{
				receiver = new OSCPortIn(OSCPortIn.defaultSCOSCPort());
			}
			else 
			{
				receiver = new OSCPortIn(OSCPortIn.defaultSCOSCPort());
			}
			
			OSCListener listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message) {
					// TODO Auto-generated method stub
					basicInputHandler.mouseMoveHandler(message.getArguments());
				}
			};
			receiver.addListener("/mouseMove", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					basicInputHandler.buttonHandler(message.getArguments(), InputEvent.BUTTON1_MASK);
				}
			};
			receiver.addListener("/leftButton", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					basicInputHandler.buttonHandler(message.getArguments(), InputEvent.BUTTON3_MASK);
				}
			};
			receiver.addListener("/rightButton", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					basicInputHandler.wheelScrollHandler(message.getArguments());
				}
			};
			receiver.addListener("/wheelScroll", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					basicInputHandler.textInputHandler(message.getArguments());
				}
			};
			receiver.addListener("/textInput", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					basicInputHandler.softKeyHandler(message.getArguments());
				}
			};
			receiver.addListener("/softKey", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					keyboardHandler.keyBoardHandler(message.getArguments());
				}
			};
			receiver.addListener("/keyboard", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					numPadHandler.numPadHandler(message.getArguments());
				}
			};
			receiver.addListener("/numpad", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					navigationHandler.navigationHandler(message.getArguments());
				}
			};
			receiver.addListener("/navigation", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					try
					{
						powerHandler.powerHandler(message.getArguments());
					} catch (IOException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
			receiver.addListener("/power", listener);
			
			listener = new OSCListener() {
				
				@Override
				public void acceptMessage(Date time, OSCMessage message)
				{
					// TODO Auto-generated method stub
					mediaHandler.mediaHandler(message.getArguments());
				}
			};
			receiver.addListener("/media", listener);
			
			receiver.startListening();
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
